package com.wangan.wanganpush.stomp.relinkThread;

import com.wangan.wanganpush.stomp.RelinkMap.ThreadMap;
import com.wangan.wanganpush.stomp.sessionHandle.BaseStompSessionHandler;
import com.wangan.wanganpush.utils.AppConstants;
import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;

/**
 * 重连线程
 */
public class LoginThread extends Thread {
	
	String  threadName = null;
	BaseStompSessionHandler clazz;
	
	public LoginThread(String threadName, BaseStompSessionHandler clazz1) {
		this.threadName = threadName;
		this.clazz = clazz1;
	}

	boolean stop = false;

	public void run() {
		
		while (!stop) {
			try {
				Thread.sleep(8000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			if (!ThreadMap.isExist(threadName)) {
				stopThread();
			}
			try {
				
				if(!stop){
				System.out.println("relinkThreadName: "+Thread.currentThread().getName());
				WebSocketClient client = new StandardWebSocketClient();
		        WebSocketStompClient stompClient = new WebSocketStompClient(client);
		        stompClient.setMessageConverter(new GenericMessageConverter());

		        stompClient.connect(AppConstants.STOMP_URL, clazz);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void stopThread() {
		stop = true;
	}

}
